import LayVmodelBase from "./LayVmodelBase.js"
import { logger, tools, runtime } from "./tools.js";

/**XmSelect 下拉组件 */
class XmSelectVmodel extends LayVmodelBase {
    config = null;
    instance = null;

    constructor(elem, config) {
        super(elem);
        this.config = config;
    }

    /**[override] */
    viewModelInit() {
        super.viewModelInit();
        let vm = this.vm;
        vm.value = ko.observableArray([]);
    }

    /**[override] */
    onDisabledChange(disabled) {
        this.instance.update({
            disabled: disabled
        });
    }

    /**[override] */
    init() {
        if (!super.init()) return;
        if (!this.config) {
            this.config = {};
        }
        var tis = this;
        let vm = this.vm;
        var config = this.config;
        config.el = this.element;

        let innerOn = config.on;
        config.on = function (data) {
            //arr:  当前多选已选中的数据
            var arr = data.arr;
            //change, 此次选择变化的数据,数组
            var change = data.change;
            //isAdd, 此次操作是新增还是删除
            var isAdd = data.isAdd;

            vm.value(data.arr.map((item) => item.value));
            innerOn && innerOn.call(this, ...arguments);
        }
        // 日期
        this.instance = xmSelect.render(config);

        //订阅value变化
        vm.value.subscribe((newValue) => {
            tis.instance.setValue(newValue);
        }, null, "change");
        this.instance.setValue(vm.value());

        this.inited = true;
    }
}

export default XmSelectVmodel;